
var calendarService = null;
var feedUri = 'https://www.google.com/calendar/feeds/default/allcalendars/full';
var scope = 'https://www.google.com/calendar/feeds/';
var requiredCalendar = ''; // Ask this one to our server.
var currentCalendarId = null; // The calendar on which operations should be performed.

var AD_METAINFO_NAME = 'ad-calendar-podio-link-event-status';
var AD_STATE_IGNORE = 'ignore';
var AD_STATE_MANAGED = 'managed';

function getCalHandler(result)
{
	var entries = result.feed.entry;

	document.getElementById('calSelect').options.length = 0;
	for (var i = 0; i < entries.length; i++)
	{
		var calendarTitle = entries[i].getTitle().getText();
		var opt = new Option(calendarTitle, calendarTitle, false, requiredCalendar == calendarTitle)

		document.getElementById('calSelect').options[document.getElementById('calSelect').options.length] = opt;

		if (requiredCalendar == calendarTitle)
			currentCalendarId = entries[i].id;
	}
}

function getEvHandler (result)
{
	var eventEntries = result.feed.getEntries();
	if (eventEntries.length > 0)
	{
		for (var i = 0; i < eventEntries.length; i++)
		{
			var event = eventEntries[i];
			var exProps = event.getExtendedProperties();
			alert(exProps.length);

			if (exProps != undefined && exProps != null && AD_METAINFO_NAME in exProps)
			{
				alert('meta is : ' + exProps[AD_METAINFO_NAME]);
				return;
			}

			var meta = new google.gdata.ExtendedProperty();
			
			meta.setName(AD_METAINFO_NAME);
			meta.setValue(AD_STATE_IGNORE);
			event.addExtendedProperty(meta);
			
			event.updateEntry(function (result) { return; }, errorHandler);
		}
	}
	else
	{
	}
}

function browseEvents()
{
	if (currentCalendarId == null)
		currentCalendarId = 'default';
	// The issue causing the current calendar to be always null has to be solved...
	var query = new google.gdata.calendar.CalendarEventQuery('https://www.google.com/calendar/feeds/' + currentCalendarId + '/private/full');
	query.setFutureEvents(true);
	
	calendarService.getEventsFeed(query, getEvHandler, errorHandler);
}

function errorHandler(e)
{
	alert("There has been an error while executing script : " + e.cause ? e.cause.statusText : e.message);
}

function ADlogin()
{
	if (!google.accounts.user.checkLogin(scope))
	{
		var token = google.accounts.user.login(scope);
		document.getElementById('linkBack').innerHTML = "Back to your calendar";
	}
	else
		alert('You are already logged in.');
}

function startService()
{
	calendarService = new google.gdata.calendar.CalendarService('calendar-podio-link-gadget');
	if (!google.accounts.user.checkLogin(scope))
		document.getElementById('logstate').innerHTML = "You should log in.";
	else if (google.accounts.user.checkLogin(scope) == undefined)
		alert('Loggin state is undefined.');
	else
	{
		document.getElementById('logstate').innerHTML = "Logged in.";
		applicationLogic();
		browseEvents();
	}
}

function applicationLogic()
{
	calendarService.getAllCalendarsFeed(feedUri, getCalHandler, errorHandler);
}

google.load("gdata", "1");
google.setOnLoadCallback(startService);
